#include "StdAfx.h"

ModelSelect* Singleton<ModelSelect>::m_instance = 0;

ModelSelect::ModelSelect()
{
	m_modelClicked = false;
	m_mouseLast.x = -1;
	m_mouseLast.y = -1;
	new BrowsingUI();
	//new SelectUI();
}

bool ModelSelect::OnMouseDown(Vertex2F pos)
{
	if(!sSelection.GetDrawMDX())
	{
		m_modelClicked = false; // just to be sure
		return false;
	}

	std::set<M2Model*>::iterator itr = sD3D.GetMDXBegin();
	bool intersected = false;
	m_pClicked = 0;

	for( ; itr != sD3D.GetMDXEnd(); ++itr)
	{
		float tmp = 0.0f;
		if((*itr)->Intersect(pos, &tmp))
		{
			m_pClicked = (*itr);
			intersected = true;
		}
	}

	m_modelClicked = intersected;
	return intersected;
}

bool ModelSelect::OnMouseUp(Vertex2F pos)
{
	if(!sSelection.GetDrawMDX())
	{
		m_modelClicked = false; // just to be sure
		return false;
	}
	if(m_modelClicked)
	{
		if(m_pClicked)
		{
			m_pClicked->SetSelected(true);
			m_lastSels.push_back(m_curSel);
			BYTE keys[256];
			GetKeyboardState(keys);
			if(keys[VK_LCONTROL] & 0x80)
				m_curSel.selectedModels.insert(m_pClicked);
			else if(keys[VK_LMENU] & 0x80)
			{
				m_pClicked->SetSelected(false);
				m_curSel.selectedModels.erase(m_pClicked);
			}
			else
			{
				std::set<M2Model*>::iterator itr = m_curSel.selectedModels.begin();
				std::set<M2Model*>::iterator end = m_curSel.selectedModels.end();

				for( ; itr != end; ++itr)
				{
					if(sD3D.IsMDXValid(*itr))
						(*itr)->SetSelected(false);
				}
				m_curSel.selectedModels.clear();
				m_curSel.selectedModels.insert(m_pClicked);
			}
		}
		m_modelClicked = false;
		return true;
	}
	else
	{
		m_lastSels.push_back(m_curSel);
		std::set<M2Model*>::iterator itr = m_curSel.selectedModels.begin();
		std::set<M2Model*>::iterator end = m_curSel.selectedModels.end();

		for( ; itr != end; ++itr)
		{
			if(sD3D.IsMDXValid(*itr))
				(*itr)->SetSelected(false);
		}
		m_curSel.selectedModels.clear();
	}
	return false;
}

bool ModelSelect::OnMouseMove(Vertex2F pos)
{
	if(!sSelection.GetDrawMDX())
		return false;

	if(m_mouseLast.x < 0)
	{
		m_mouseLast = pos;
		return false;
	}

	float deltaX = pos.x - m_mouseLast.x;
	float deltaY = pos.y - m_mouseLast.y;
	
	m_mouseLast = pos;

	if(!deltaX && !deltaY)
		return false;

	if(!m_curSel.selectedModels.size())
		return false;

	BYTE keys[256];
	GetKeyboardState(keys);

	if(!(keys[VK_LCONTROL] & 0x80) && !(keys[VK_LMENU] & 0x80) && !(keys[VK_LSHIFT] & 0x80))
		return false;

	float diffX, diffY, diffZ;
	diffX = diffY = diffZ = 0.0f;

	if(keys[VK_LCONTROL] & 0x80)
		diffX += deltaX * 0.5f;
	else if(keys[VK_LMENU] & 0x80)
		diffY += deltaY * 0.5f;
	else if(keys[VK_LSHIFT] & 0x80)
		diffZ += deltaX * 0.5f;

	sCamera.UpdatePos(pos);
	return true;


	std::set<M2Model*>::iterator itr = m_curSel.selectedModels.begin();
	std::set<M2Model*>::iterator end = m_curSel.selectedModels.end();

	for( ; itr != end; ++itr)
	{
		if(sD3D.IsMDXValid(*itr))
			(*itr)->ModTranslation(diffX, diffY, diffZ);
	}

	sCamera.UpdatePos(pos);
	return true;
}